package com.zyx.crawlerdemo.htmlxmlparse.htmlcleaner;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

/**
 * @author Yaxi.Zhang
 * @since 2021/8/12 10:03
 * desc: htmlcleaner测试案例2
 */
public class HtmlcleanerTest2 {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws IOException, XPatherException {
		URL url=new URL("http://www.********.com.cn/b.asp");
		// 实例化HtmlCleaner
		HtmlCleaner cleaner = new HtmlCleaner(); 
		// 转化成TagNode
		TagNode node = cleaner.clean(url,"gbk"); 
		// 遍历获取课程名以及课程地址
		// 这里使用//a表示不考虑位置,如果使用/a获取不到内容
		Object[]  ns2 = node.evaluateXPath("//*[@id='course']/ul//a");
		for(Object on : ns2) {  
			TagNode n = (TagNode) on;  
			System.out.println("课程名为:\t" + n.getText() + "\t地址为:\t" + n.getAttributeByName("href"));  
		} 
		TagNode n = (TagNode) ns2[0]; 
		TagNode tagNodePa = n.getParent();
		List<TagNode> tagNodeChi = tagNodePa.getChildTagList();

	}
}
